<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<div id="role_content_div">
	<div id="role_condition_div">
		<table style="width:100%; border-collapse:separate; border-spacing:5px;">
			<tr>
				<td>
					<input id="roleCode">
				</td>
				<td>
					<input id="roleName">
				</td>
			</tr>
			<tr>
				<td colspan="2">
					<a id="role_search_btn" href="javascript:void(0);"></a>
					<a id="role_privilege_btn" href="javascript:void(0);"></a>
				</td>
			</tr>
		</table>
	</div>
	<div id="role_list_div">
		<table id="role_list_table"></table>
	</div>
</div>
<script type="text/javascript">
	$(function() {
		initRoleConditionDiv();
		initRoleListTable();
	});
	
	/** 初始化角色查询条件DIV  */
	function initRoleConditionDiv() {
		$("#role_condition_div").panel({
			title : "查询条件",
			border : false
		});
		$("#role_list_div").panel({
			title : "角色列表",
			border : false
		});
		$("#roleCode").textbox({
			label : "角色编码：",
			labelAlign : "right",
			width : "300px",
			labelWidth : "70px"
		});
		$("#roleName").textbox({
			label : "角色名称：",
			labelAlign : "right",
			width : "300px",
			labelWidth : "70px"
		});
		$("#role_search_btn").linkbutton({
			text : "查询",
			iconCls : "icon-zoom",
			onClick : function() {
				initRoleListTable();
			}
		});
		$("#role_privilege_btn").linkbutton({
			text : "添加菜单",
			iconCls : "icon-folder_table",
			onClick : function() {
				$.messager.alert("提示信息", "添加菜单功能待实现", "info");
			}
		});
	} 
	
	/** 初始化角色列表Table  */
	function initRoleListTable() {
		var params = new Object();
		params.roleCode = $("#roleCode").textbox("getValue");
		params.roleName = $("#roleName").textbox("getValue");
		$("#role_list_table").datagrid({
			url : "${pageContext.request.contextPath}/system/initRoleTable",
			pagination : true,
			singleSelect : true,
			width : "auto",
			height : "auto",
			fitColumns : true,
			pageList : [10,20,30,40,50],
			queryParams : params,
			idField : "roleId",
			columns : [[
				{field:"roleId",title:"角色ID",width:50},
				{field:"roleCode",title:"角色编码",width:100},
				{field:"roleName",title:"角色名称",width:150},
				{field:"rcState",title:"状态",width:100},
				{field:"createUserName",title:"创建人",width:150},
				{field:"createDate",title:"创建时间",width:150},
				{field:"modifyUserName",title:"修改人",width:150},
				{field:"modifyDate",title:"修改时间",width:150}
			]],
			toolbar : [{
				iconCls : "icon-add",
				handler : function() {
					$("#role_content_div").append("<div id='role_add_dialog'></div>");
					$("#role_add_dialog").dialog({
						title : "添加角色",
						closable : false,
						modal : true,
						width : "700px",
						height : "150px",
						cache : false,
						href : "${pageContext.request.contextPath}/system/roleAddPage",
						buttons : [{
							text : "添加",
							handler : function() {
								var roleCode = $.trim($("#role_add_code").textbox("getValue"));
								var roleName = $.trim($("#role_add_name").textbox("getValue"));
								if (roleCode == "" || roleName == "") {
									$.messager.alert("异常信息", "角色编码和名称必填！", "error");
									return;
								}
								var roleAdd = {};
								roleAdd.roleCode = roleCode;
								roleAdd.roleName = roleName;
								$.post("${pageContext.request.contextPath}/system/roleAdd",
									roleAdd,
									function(data) {
										if (data.flag) {
											$.messager.alert("提示信息", data.msg, "info");
											$("#role_add_dialog").dialog("destroy");
											initRoleListTable();
										} else {
											$.messager.alert("异常信息", data.msg, "error");
										}
									},"JSON"
								);
							}
						}, {
							text : "取消",
							handler : function() {
								$("#role_add_dialog").dialog("destroy");
							}
						}]
					});
				}
			}, {
				iconCls : "icon-edit",
				handler : function() {
					var selectedRow = $("#role_list_table").datagrid("getSelected");
					if (!selectedRow) {
						$.messager.alert("异常信息", "请选择要修改的行！", "error");
						return;
					}
					$("#role_content_div").append("<div id='role_modify_dialog'></div>");
					$("#role_modify_dialog").dialog({
						title : "修改角色",
						closable : false,
						modal : true,
						width : "700px",
						height : "150px",
						cache : false,
						href : "${pageContext.request.contextPath}/system/roleModifyPage",
						buttons : [{
							text : "修改",
							handler : function() {
								var roleName = $.trim($("#role_modify_name").textbox("getValue"));
								if (roleName == "") {
									$.messager.alert("异常信息", "角色名称必填！", "error");
									return;
								}
								var roleModify = {};
								roleModify.roleName = roleName;
								roleModify.roleId = selectedRow.roleId;
								$.post("${pageContext.request.contextPath}/system/roleModify",
									roleModify,
									function(data) {
										if (data.flag) {
											$.messager.alert("提示信息", data.msg, "info");
											$("#role_modify_dialog").dialog("destroy");
											initRoleListTable();
										} else {
											$.messager.alert("异常信息", data.msg, "error");
										}
									},"JSON"
								);
							}
						}, {
							text : "取消",
							handler : function() {
								$("#role_modify_dialog").dialog("destroy");
							}
						}]
					});
				}
			}, {
				iconCls : "icon-delete",
				handler : function() {
					var selectedRow = $("#role_list_table").datagrid("getSelected");
					if (!selectedRow) {
						$.messager.alert("异常信息", "请选择要删除的行！", "error");
						return;
					}
					$.messager.confirm("确认对话框", "您确实要删除这条数据吗？", function(r) {
						if (r) {
							$.post("${pageContext.request.contextPath}/system/roleDelete",
								{roleId : selectedRow.roleId},
								function(data) {
									if (data.flag) {
										$.messager.alert("提示信息", data.msg, "info");
										initRoleListTable();
									} else {
										$.messager.alert("异常信息", data.msg, "error");
									}
								},"JSON"
							);
						}
					});
				}
			}]
		});
	}
</script>